package threadtest;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/*
socket 英文 插座，孔
 */
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/*
客户端练习
 */
public class Client {
    private Socket socket;
    public Client(){
        try {//创建"电话"实例 输入地址 和端口号
            socket = new Socket("localhost",9090);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {//调用方法
            System.out.println("正在连接");
            OutputStream os = socket.getOutputStream();
            System.out.println("连接成功");
            OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);//底层8kb的byte数组不满要强制输出

            while (true) {
                System.out.println("请输入要发送的信息");
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                                System.in
                        )
                );
                String s = br.readLine();
                if ("exit".equals(s)){
                    System.out.println("输入关闭");
                    pw.close();
                }
                pw.println(s);
            }





        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

    }

}
